package com.block.common.util;

import com.alibaba.fastjson.JSON;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.security.util.ApiUtil;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class HttpReturnUtil {

    /**
     *
     * @param response
     * @param appEncrypt
     * @param respCode
     * @param respMsg
     * @param date
     * @throws Exception
     */
    public static void returnJson(HttpServletResponse response, AppEncrypt appEncrypt, String respCode, String respMsg , Date date, String uri) throws Exception{

        ResponseVo responseVo=null;
        if(ValidateUtil.validateStrNullValue(respMsg)) {
             responseVo = new ResponseVo(respCode);
        }else {
            responseVo = new ResponseVo(respCode, respMsg, "");
        }
         String resp= JSON.toJSONString(responseVo);

        if(appEncrypt!=null) {
             resp = ApiUtil.dealReturnJson(responseVo, appEncrypt, "", date,uri);
        }
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(resp);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }

    }

    /**
     * 拦截器返回数据处理方法
     * @param response
     * @param appEncrypt
     * @param responseVo
     * @param date
     * @param uri
     * @throws Exception
     */
    public static void returnJson(HttpServletResponse response, AppEncrypt appEncrypt, ResponseVo responseVo , Date date, String uri) throws Exception{


        String resp= JSON.toJSONString(responseVo);

        if(appEncrypt!=null) {
            resp = ApiUtil.dealReturnJson(responseVo, appEncrypt, "", date,uri);
        }
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(resp);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }


    /**
     * 扩展渠道的返回信息
     *
     */
    public static void returnJson(HttpServletResponse response, ResultInfoVO resultInfoVO){

        String resp= JSON.toJSONString(resultInfoVO);
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(resp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }

    }

    /**
     *
     *
     */
    public static void returnJson(HttpServletResponse response, String requestNo, ResultInfoVO resultInfoVO){

        resultInfoVO.setRequestNo(requestNo);
        String resp= JSON.toJSONString(resultInfoVO);
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(resp);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static String returnJson(String uri, String requestNo, ResultInfoVO resultInfoVO){
        resultInfoVO.setRequestNo(requestNo);
        String resp= JSON.toJSONString(resultInfoVO);
        LogFactory.getSourceLog().info("【uri】{}【返回参数】{}", uri, resp);
        return resp;
    }

}
